package com.example.imtoken.modules.main.wallet.details.trade.details.share

import android.app.Dialog
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.IntentUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.imtoken.R
import com.example.imtoken.core.utils.MyImageLoaderUtils
import com.example.imtoken.databinding.TransferDetailsShareDialogFragmentBinding
import com.example.imtoken.entitys.http.TransactionsEntityItem
import com.example.imtoken.extend.click
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * 转账详情分享
 * @property viewModel TransferDetailsShareDialogViewModel
 */
class TransferDetailsShareDialogFragment(private val transactionsEntityItem: TransactionsEntityItem) :
    BottomSheetDialogFragment() {


    private lateinit var dataBinding: TransferDetailsShareDialogFragmentBinding
    private var mBehavior: BottomSheetBehavior<View>? = null

    private var uri: Uri? = null

    private val viewModel by lazy {
        ViewModelProvider(this).get(TransferDetailsShareDialogViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.AppBottomSheet)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dataBinding = DataBindingUtil.inflate(
            layoutInflater,
            R.layout.transfer_details_share_dialog_fragment,
            null,
            false
        )
        dialog.setContentView(dataBinding.root)
        val lp = dataBinding.root.layoutParams
        lp.height = ScreenUtils.getAppScreenHeight() - ConvertUtils.sp2px(50f)
        dataBinding.root.layoutParams = lp
        mBehavior = BottomSheetBehavior.from(dataBinding.root.parent as View)
        mBehavior?.addBottomSheetCallback(mBottomSheetBehaviorCallback)
        initViewContent()
        return dialog
    }

    private fun initViewContent() {
        viewModel.setData(transactionsEntityItem)
        initListener()
        initObserver()
    }

    private fun initListener() {
        dataBinding.run {
            tvCancel.click {
                dismiss()
            }
            tvShare.click {
                try {
                    if (uri == null) {
                        val view = dataBinding.clContent
                        val bitmap = ConvertUtils.view2Bitmap(view)
                        uri = MyImageLoaderUtils.saveImageToGallery(bitmap, requireActivity())
                        ToastUtils.showShort("保存图片成功,开始分享")
                    }
                    val intent = IntentUtils.getShareImageIntent(uri)
                    requireActivity().startActivity(intent)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
    }

    private fun initObserver() {
        viewModel.data.observe(this) {
            dataBinding.info = it
        }
    }

    override fun onStart() {
        super.onStart()
        mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
    }

    private val mBottomSheetBehaviorCallback = object : BottomSheetBehavior.BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, newState: Int) {
            if (newState == BottomSheetBehavior.STATE_DRAGGING)
                mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) {

        }

    }
}